package com.cynteka.cyfoman.core.config;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
import java.util.regex.Pattern;

/**
 * User: Sergey
 * Date: 28.07.11
 * Time: 10:17
 */
@XmlRootElement
public class ExplorerRowType {
    private String regexp;
    private transient Pattern pattern;
    private List<String> columnNames;
    private String icon;

    public ExplorerRowType() {
    }

    public ExplorerRowType(String regexp, List<String> columnNames) {
        this.regexp = regexp;
        this.columnNames = columnNames;
    }

    @XmlElement
    public String getRegexp() {
        return regexp;
    }

    public void setRegexp(String regexp) {
        this.regexp = regexp;
        recompile();
    }

    public Pattern getPattern() {
        return pattern;
    }

    @XmlElement
    public List<String> getColumnNames() {
        return columnNames;
    }

    public void setColumnNames(List<String> columnNames) {
        this.columnNames = columnNames;
    }

    @XmlElement
    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ExplorerRowType that = (ExplorerRowType) o;

        if (columnNames != null ? !columnNames.equals(that.columnNames) : that.columnNames != null) return false;
        if (icon != null ? !icon.equals(that.icon) : that.icon != null) return false;
        if (regexp != null ? !regexp.equals(that.regexp) : that.regexp != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = regexp != null ? regexp.hashCode() : 0;
        result = 31 * result + (columnNames != null ? columnNames.hashCode() : 0);
        result = 31 * result + (icon != null ? icon.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "ExplorerRowType{" +
                "regexp='" + regexp + '\'' +
                ", columnNames=" + columnNames +
                ", icon='" + icon + '\'' +
                '}';
    }

    private void recompile() {
        pattern = Pattern.compile(regexp);
    }
}
